/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.bomt.listener;

import com.comac.ins.bomt.domain.*;
import com.comac.ins.bomt.event.*;
import com.comac.ins.bomt.service.BizFileService;
import com.comac.ins.bomt.service.BomGenerateService;
import com.comac.ins.common.core.annotations.BizEventListener;
import com.comac.ins.common.core.domain.model.BizEvent;
import com.comac.ins.common.core.utils.BizAssertUtil;
import com.comac.ins.common.json.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * BOM事件监听器
 *
 * @author zhanghongyi
 * @date 2024/8/8 13:16
 * @description BomEventListener
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BomEventListener {
    @Value("${bom.file.versionKeepNum:5}")
    private Integer fileVersionKeepNum;

    private final BizFileService bizFileService;

    private final BomGenerateService bomGenerateService;

    @BizEventListener(async = false)
    public void bomFileModifyListener(BizEvent<BomFileModifyEvent> bizEvent) {
        BomFileModifyEvent event = bizEvent.getEventPayload();
        if (StringUtils.isNotBlank(event.getBomInfo().getExtraInfo())) {
            BomExtraInfo extraInfoObj = JsonUtils.parseObject(event.getBomInfo().getExtraInfo(), BomExtraInfo.class);
            BizAssertUtil.notNull(extraInfoObj, "parse extra info fail");
            List<Long> cleanFileIdList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(extraInfoObj.getStandardBomFileHistory()) && extraInfoObj.getStandardBomFileHistory().size() > fileVersionKeepNum) {
                int cutPoint = extraInfoObj.getStandardBomFileHistory().size() - fileVersionKeepNum;
                cleanFileIdList.addAll(extraInfoObj.getStandardBomFileHistory().subList(0, cutPoint));
                extraInfoObj.setStandardBomFileHistory(extraInfoObj.getStandardBomFileHistory().subList(cutPoint, extraInfoObj.getStandardBomFileHistory().size()));
            }
            if (CollectionUtils.isNotEmpty(extraInfoObj.getStrengthBomFileHistory()) && extraInfoObj.getStrengthBomFileHistory().size() > fileVersionKeepNum) {
                int cutPoint = extraInfoObj.getStrengthBomFileHistory().size() - fileVersionKeepNum;
                cleanFileIdList.addAll(extraInfoObj.getStrengthBomFileHistory().subList(0, cutPoint));
                extraInfoObj.setStrengthBomFileHistory(extraInfoObj.getStrengthBomFileHistory().subList(cutPoint, extraInfoObj.getStrengthBomFileHistory().size()));
            }
            if (CollectionUtils.isNotEmpty(extraInfoObj.getStrengthVsStandardFileHistory()) && extraInfoObj.getStrengthVsStandardFileHistory().size() > fileVersionKeepNum) {
                int cutPoint = extraInfoObj.getStrengthVsStandardFileHistory().size() - fileVersionKeepNum;
                cleanFileIdList.addAll(extraInfoObj.getStrengthVsStandardFileHistory().subList(0, cutPoint));
                extraInfoObj.setStrengthVsStandardFileHistory(extraInfoObj.getStrengthVsStandardFileHistory().subList(cutPoint, extraInfoObj.getStrengthVsStandardFileHistory().size()));
            }
            if (CollectionUtils.isNotEmpty(extraInfoObj.getStatusVsStrengthFileHistory()) && extraInfoObj.getStatusVsStrengthFileHistory().size() > fileVersionKeepNum) {
                int cutPoint = extraInfoObj.getStatusVsStrengthFileHistory().size() - fileVersionKeepNum;
                cleanFileIdList.addAll(extraInfoObj.getStatusVsStrengthFileHistory().subList(0, cutPoint));
                extraInfoObj.setStatusVsStrengthFileHistory(extraInfoObj.getStatusVsStrengthFileHistory().subList(cutPoint, extraInfoObj.getStatusVsStrengthFileHistory().size()));
            }
            for (Long fileId : cleanFileIdList) {
                bizFileService.delete(event.getLoginUser(), fileId);
            }
        }

    }

    ;

    @BizEventListener(async = false)
    public void standardBomGenerate(BizEvent<StandardBomGenerateEvent> event) {
        bomGenerateService.doStandardBomGenerate(event.getEventPayload());
    }

    @BizEventListener(async = false)
    public void strengthBomGenerate(BizEvent<StrengthBomGenerateEvent> event) {
        bomGenerateService.doStrengthBomGenerate(event.getEventPayload());
    }

    @BizEventListener(async = false)
    public void statusStrengthCompare(BizEvent<ModelStatusStrengthCompareEvent> event) {
        bomGenerateService.doStatusStrengthCompareGenerate(event.getEventPayload());
    }

    @BizEventListener(async = false)
    public void strengthStandardBomCompare(BizEvent<StrengthStandardCompareEvent> event) {
        bomGenerateService.doStrengthStandardCompareGenerate(event.getEventPayload());
    }
}
